{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "人脸数： 1\n",
      "脸型为： oval\n",
      "脸型的置信水平： 0.38\n",
      "[{'x': 2388.14, 'y': 391.23}, {'x': 2389.77, 'y': 436.33}, {'x': 2399.26, 'y': 480.56}, {'x': 2419.73, 'y': 523.57}, {'x': 2448.32, 'y': 558.78}, {'x': 2484.32, 'y': 582.28}, {'x': 2521.25, 'y': 599.84}, {'x': 2553.06, 'y': 581.21}, {'x': 2583.16, 'y': 556.02}, {'x': 2607.01, 'y': 522.33}, {'x': 2623.84, 'y': 482.82}, {'x': 2633.28, 'y': 442.65}, {'x': 2636.62, 'y': 401.72}, {'x': 2503.02, 'y': 381.81}, {'x': 2506.75, 'y': 379.51}, {'x': 2511.64, 'y': 377.78}, {'x': 2516.58, 'y': 378.8}, {'x': 2519.81, 'y': 382.4}, {'x': 2516.77, 'y': 383.3}, {'x': 2512.55, 'y': 382.79}, {'x': 2507.79, 'y': 382.93}, {'x': 2513.98, 'y': 381.08}, {'x': 2497.31, 'y': 355.65}, {'x': 2507.33, 'y': 349.91}, {'x': 2516.5, 'y': 346.47}, {'x': 2527.1, 'y': 346.11}, {'x': 2534.38, 'y': 354.87}, {'x': 2526.65, 'y': 354.96}, {'x': 2517.53, 'y': 355.66}, {'x': 2507.93, 'y': 356.63}, {'x': 2540.32, 'y': 385.67}, {'x': 2544.94, 'y': 384.4}, {'x': 2548.83, 'y': 384.79}, {'x': 2553.84, 'y': 388.44}, {'x': 2556.24, 'y': 391.7}, {'x': 2551.09, 'y': 391.61}, {'x': 2545.98, 'y': 389.73}, {'x': 2542.58, 'y': 387.94}, {'x': 2549.59, 'y': 388.03}, {'x': 2528.56, 'y': 359.39}, {'x': 2537.4, 'y': 352.21}, {'x': 2547.71, 'y': 353.75}, {'x': 2557.59, 'y': 359.16}, {'x': 2565.06, 'y': 366.56}, {'x': 2554.35, 'y': 366.36}, {'x': 2545.67, 'y': 363.64}, {'x': 2536.66, 'y': 361.48}, {'x': 2523.55, 'y': 387.59}, {'x': 2520.47, 'y': 411.19}, {'x': 2515.49, 'y': 434.68}, {'x': 2512.52, 'y': 457.47}, {'x': 2526.46, 'y': 467}, {'x': 2533.62, 'y': 465.13}, {'x': 2549.05, 'y': 458.29}, {'x': 2544.59, 'y': 434.61}, {'x': 2537.82, 'y': 411.39}, {'x': 2533.14, 'y': 388.24}, {'x': 2531.37, 'y': 438.43}, {'x': 2508.95, 'y': 516.31}, {'x': 2515.17, 'y': 503}, {'x': 2524.12, 'y': 496.16}, {'x': 2528.62, 'y': 507.74}, {'x': 2529.91, 'y': 523.04}, {'x': 2521.93, 'y': 526.27}, {'x': 2521.36, 'y': 528.39}, {'x': 2516.17, 'y': 522.54}, {'x': 2517.67, 'y': 510.02}, {'x': 2523.04, 'y': 507.63}, {'x': 2523.34, 'y': 514.93}, {'x': 2521.23, 'y': 517.74}, {'x': 2521.1, 'y': 516.89}, {'x': 2517.55, 'y': 514.72}]\n"
     ]
    }
   ],
   "source": [
    "import base64\n",
    "import json\n",
    "import requests\n",
    " \n",
    "class BaiduPicIndentify:\n",
    "    def __init__(self,img):\n",
    "        self.AK = \"3ooIrdR1rI8XX1oY52TLnGST\"\n",
    "        self.SK = \"08ySYCOcXx1mrAHoTUdD6Qus4akn37Bt\"\n",
    "        self.img_src = img\n",
    "        self.headers = {\n",
    "            \"Content-Type\": \"application/json; charset=UTF-8\"\n",
    "        }\n",
    " \n",
    "    def get_accessToken(self):\n",
    "        host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + self.AK + '&client_secret=' + self.SK\n",
    "        response = requests.get(host, headers=self.headers)\n",
    "        json_result = json.loads(response.text)\n",
    "        return json_result['access_token']\n",
    " \n",
    "    def img_to_BASE64(self,path):\n",
    "        with open(path,'rb') as f:\n",
    "            base64_data = base64.b64encode(f.read())\n",
    "            return base64_data\n",
    " \n",
    "    def detect_face(self):\n",
    "        # 人脸检测与属性分析\n",
    "        # square: 正方形 triangle:三角形 oval: 椭圆 heart: 心形 round: 圆形\n",
    "        img_BASE64 = self.img_to_BASE64(self.img_src)\n",
    "        request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "        post_data = {\n",
    "            \"image\": img_BASE64,\n",
    "            \"image_type\": \"BASE64\",\n",
    "            \"face_field\": \"face_shape,landmark\",\n",
    "            \"face_type\": \"LIVE\"\n",
    "        }\n",
    "        access_token = self.get_accessToken()\n",
    "        request_url = request_url + \"?access_token=\" + access_token\n",
    "        response = requests.post(url=request_url, data=post_data, headers=self.headers)\n",
    "        json_result = json.loads(response.text)\n",
    "        if json_result['error_msg']!='pic not has face':\n",
    "            print(\"人脸数：\", json_result['result']['face_num'])\n",
    "            print(\"脸型为：\",json_result['result']['face_list'][0]['face_shape']['type'])\n",
    "            print(\"脸型的置信水平：\",json_result['result']['face_list'][0]['face_shape']['probability'])\n",
    "            print(json_result['result']['face_list'][0]['landmark72'])\n",
    "            \n",
    " \n",
    "if __name__=='__main__':\n",
    "    img_src='images/test.jpg'\n",
    "    baiduDetect = BaiduPicIndentify(img_src)\n",
    "    baiduDetect.detect_face()\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
